﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Domain.Repositories;

namespace Himp.TaskScheduling.Datas
{
    /// <summary>
    /// 制样机系统操作记录
    /// </summary>
    public class ZYOperateRecordService : TaskSchedulingAppService, IZYOperateRecordService
    {
        private readonly IRepository<ZYOperateRecord, long> _zyOperateRecordRepository;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="msgSendRecRepository"></param>
        /// <param name="hubContext"></param>
        public ZYOperateRecordService(IRepository<ZYOperateRecord, long> zyOperateRecordRepository)
        {
            _zyOperateRecordRepository = zyOperateRecordRepository;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public async Task<PagedResultDto<ZYOperateRecordDto>> GetListAsync(ZYOperateRecordInput input)
        {
            var iQuery = await _zyOperateRecordRepository.GetQueryableAsync();
            iQuery = iQuery.WhereIf(input.BegnTime.HasValue, i => i.DateTime.Date >= input.BegnTime.Value.Date);
            iQuery = iQuery.WhereIf(input.EndTime.HasValue, i => i.DateTime.Date <= input.EndTime.Value.Date);
            iQuery = iQuery.OrderByDescending(i => i.DateTime);

            if (input.Pi == 0)
            {
                var itemsDto = ObjectMapper.Map<List<ZYOperateRecord>, List<ZYOperateRecordDto>>(iQuery.ToList());

                return new PagedResultDto<ZYOperateRecordDto>(itemsDto.Count, itemsDto);
            }
            else
            {
                var totalCount = iQuery.Count();
                var items = iQuery.Page(input.Pi, input.Ps);
                var itemsDto = ObjectMapper.Map<List<ZYOperateRecord>, List<ZYOperateRecordDto>>(items.ToList());

                return new PagedResultDto<ZYOperateRecordDto>(totalCount, itemsDto);
            }
        }
    }
}
